package uk.co.q3c.deplan.client.util;

public class Q3StringUtil {

	/**
	 * Null safe case sensitive. Null counts as lowest possible value
	 * 
	 * @return
	 */
	public static int compare(String a, String b) {
		if ((a != null) && (b != null)) {
			return a.compareTo(b);
		}
		if ((a == null) && (b == null)) {
			return 0;
		}
		if (a == null) {
			return -1;
		}
		if (b == null) {
			return 1;
		}
		throw new RuntimeException("unexpected compare failure");
	}

	/**
	 * returns a string split at camel case points - for example "camelCase" becomes "camel case"
	 * 
	 * @param s
	 * @return
	 */
	public static String splitAtCamelCase(String s) {
		StringBuilder buf = new StringBuilder();
		for (int i = 0; i < s.length(); i++) {
			Character c = s.charAt(i);
			if (Character.isUpperCase(c)) {
				if (i == 0) {
					buf.append(c);
				} else {
					buf.append(" ");
					buf.append(Character.toLowerCase(c));
				}
			} else {
				buf.append(c);
			}

		}
		return buf.toString();
	}

	/**
	 * Needed by GWT because the simpleName method of class is not available
	 * 
	 * @param className
	 * @return
	 */
	public static String simpleNameFromClassName(String className) {
		int c = className.lastIndexOf(".");
		String s = className.substring(c + 1);
		return s;
	}

	public static String methodNamefromfieldName(String prefix, String name) {
		StringBuffer buf = new StringBuffer(prefix);
		buf.append(Character.toUpperCase(name.charAt(0)));
		buf.append(name.substring(1));
		return buf.toString();
	}

	public static String instanceNameFromClass(String className) {
		StringBuffer buf = new StringBuffer();
		buf.append(Character.toLowerCase(className.charAt(0)));
		buf.append(className.substring(1));
		return buf.toString();
	}

	public static String lowerCaseFirstChar(String s) {
		return s.substring(0, 1)
				.toLowerCase() + s.substring(1, s.length());
	}

}
